﻿/**	key_bind.hpp
 *	----	Create Time Stamp	--2006-05-13 09:59:02--
 *-----------------------------------------------------------------------------
 *	Mean	:整数型ID分配器
 *	Author	:Nick Shallery	(nicknide@gmail.com)
 *	Version	:1.0.43.75	(2009-11-10 22:18:11)
 *-----------------------------------------------------------------------------
**/	/*YOU HAVE ALL LEFT WITH THIS CODE WHILE YOU KEEP COMMENT IN EVERY COPY.*/


#if			!defined(__LOLIX__TOY__KEY_BIND__K_IN_G__)
#define		__LOLIX__TOY__KEY_BIND__K_IN_G__

#if			!defined(__LOLIX__LOLI_X_CFG__K_IN_G__)
	#include	"../lolix_cfg.h"
#endif	//	!defined(__LOLIX__LOLI_X_CFG__K_IN_G__)
#if			!defined(__LOLIX__TOY__ALIGN__HPP__NICK__K_IN_G__)
	#include	"./align.hpp"
#endif	//	!defined(__LOLIX__TOY__ALIGN__HPP__NICK__K_IN_G__)
#if			!defined(__LOLIX__TOY_ALLOCATOR__K_IN_G__)
	#include	"./allocator.h"
#endif	//	!defined(__LOLIX__TOY_ALLOCATOR__K_IN_G__)
#if			!defined(__LOLIX__TOY__PAIR_TYPE__K_IN_G__)
	#include	"./pair_type.hpp"
#endif	//	!defined(__LOLIX__TOY__PAIR_TYPE__K_IN_G__)
#if			!defined(__LOLIX__TOY__REBIND_TYPE__K_IN_G__)
	#include	"./rebind_type.hpp"
#endif	//	!defined(__LOLIX__TOY__REBIND_TYPE__K_IN_G__)
#if			!defined(__LOLIX__TOY__STD_VECTOR__NICK__K_IN_G__)
	#include	"./std_vector.hpp"
#endif	//	!defined(__LOLIX__TOY__STD_VECTOR__NICK__K_IN_G__)
#if			!defined(__LOLIX__LOLI_X_CFG__K_IN_G__)
	#include	"../lolix_type.h"
#endif	//	!defined(__LOLIX__LOLI_X_CFG__K_IN_G__)


#include	<iterator>

namespace lolix{namespace toy{
	LOLIX_ALLOC("lolix::toy::key_bind", KEY_BIND_DEFAULT_ALLOC);
	template <typename bind_type, size_type max_element_size, size_type row_size_input, typename _T_ALLOC = KEY_BIND_DEFAULT_ALLOC<void>, typename need_adjust_free_prev_pointer = type_false>
	class key_bind;

	template <typename bind_type, size_type max_element_size, size_type row_size_input, typename _T_ALLOC = KEY_BIND_DEFAULT_ALLOC<void> >
	class key_bind_random;

	class	LOLIX_NO_VTABLE	key_bind_dynamic_handle;

	namespace _help
	{
		template<size_type max_element_size, size_type row_size_input, typename _TAlc, typename _TDev> class key_bind_base;
		class key_bind_archive;
	}
	template <size_type max_element_size, size_type row_size_input, typename _T_ALLOC = KEY_BIND_DEFAULT_ALLOC<void> >
	class id_ctrl
		: public key_bind<void, max_element_size, row_size_input, typename _T_ALLOC::rebind<void>::other >{};

	template < size_type max_size, typename _Tx >
	class key_type;

	template < typename _Ty, typename _TKey >
	class key_bind_dynamic;


	enum	ENUM_KEY_BIND_STRATEGY
	{	KEY_BIND_STRATEGY_FAST		= 0
	,	KEY_BIND_STRATEGY_NORMAL	= 1
	,	KEY_BIND_STRATEGY_SAFE		= 2
	,	KEY_BIND_STRATEGY_VER		= 0
	};

}}


template<lolix::size_type max_size, typename _Tx>
class lolix::toy::key_type
{
	friend	_help::key_bind_archive;

	friend _Tx;
	typedef key_type<max_size, _Tx> this_type;
	typedef typename lolix::toy::size_info<max_size>::size_type _k_type;
	class no_usd;
public:
	typedef	_k_type	value_type;
	_k_type _key;
	LOLIX_INLINE_CALL key_type<max_size, _Tx>(void){}
	LOLIX_INLINE_CALL key_type<max_size, _Tx>(const key_type<max_size, _Tx>& key): _key(key._key){}
	//key_type<max_size, _Tx>(key_type<max_size, _Tx>& key): _key(key._key){}

	LOLIX_INLINE_CALL bool operator == (const this_type key)const	{ return _key == key._key; }
	LOLIX_INLINE_CALL bool operator != (const this_type key)const	{ return _key != key._key; }
	LOLIX_INLINE_CALL this_type& operator = (const this_type key){ _key = key._key; return *this; };

	template<typename _Ty>	LOLIX_INLINE_CALL bool operator == (const _Ty other) const { return _key == other; }
	template<typename _Ty>	LOLIX_INLINE_CALL bool operator != (const _Ty other) const { return _key != other; }

	LOLIX_INLINE_CALL void go_bad(void){ _key = max_size; }
	LOLIX_INLINE_CALL bool is_bad(void)const{ return _key >= max_size; }
	LOLIX_INLINE_CALL bool is_valid(void)const{ return !is_bad(); }

	LOLIX_INLINE_CALL static this_type get_bad(void){ key_type tmp; tmp.go_bad(); return tmp; }
	struct compare_type
	{
		LOLIX_INLINE_CALL bool operator()(this_type const& l, this_type const& r)const
			{
			return l._key < r._key;
			}
	};
private:
	template<typename _Ty>	LOLIX_INLINE_CALL key_type<max_size, _Tx>(_Ty& key): _key(key){}
	template<typename _Ty>	LOLIX_INLINE_CALL this_type& operator = (_Ty& key){ _key = key; return *this; }
	template<typename _Ty>	LOLIX_INLINE_CALL operator _Ty(void)const{ return static_cast<_Ty>(_key); STATIC_ASSERT(sizeof(_Ty) >= sizeof(_k_type)); }
};



template<lolix::size_type max_element_size, lolix::size_type row_size_input, typename _TAlc, typename _TDev>
class lolix::toy::_help::key_bind_base
{
	CANNT_COPY_LOLI;
	typedef	_TDev	device_type;
	typedef key_bind_base<max_element_size, row_size_input, _TAlc, _TDev> this_type;
	enum { _e_row_size = 1 << size_info<(row_size_input>>1)>::size_bits/*element_size/row_cnt_input*/ };						//	每行的数量
	enum { element_size = high_align<_e_row_size, max_element_size>::align_value };	//	向上对齐总数到行数的整数倍..
	enum { _e_row_cnt = element_size/_e_row_size };									//	累计有多少行
protected:
	typedef typename size_info<_e_row_size>::size_type type_row_size;	//	行成员容量的类型
	typedef typename size_info<_e_row_cnt>::size_type type_row_cnt;		//	行数量的类型
public:
	typedef	_TAlc	allocator_type;
	static const type_row_size	row_size = _e_row_size;
	static const type_row_cnt	row_cnt = _e_row_cnt;
	ENUM_KEY_BIND_STRATEGY	mode(void)const
		{
		return _bind_mode;
		}

protected:
	enum { max_capacity = row_size*row_cnt };							//	最大可容纳数量 >= max_element_size
	typedef typename size_info<max_capacity>::size_type type_offset;
	enum { type_offset_bits = size_info<max_capacity>::size_bits };		//	最大容量数量所需要的位的数量 : 可支持 bad_key 的位数
public:
	enum { space_size = max_capacity };
	enum { bad_key = space_size };
	enum { key_type_bits = type_offset_bits };

	typedef type_offset	size_type;
	typedef lolix::toy::key_type<bad_key, this_type> key_type;
protected:
	static type_offset __get_offset_by_key__(key_type k){ return (type_offset)k; }
	static key_type __get_key_by_offset__(type_offset offset){ return key_type(offset); }


	struct type_link
		{
		type_offset	_prev : type_offset_bits;							//	前一个节点的位置 : 空闲链时保留
		type_offset	_next : type_offset_bits;							//	后一个节点的位置
		type_offset	_usd  : 1;											//	是否被使用
		CANNT_COPY_LOLI;
		};
	#pragma	region	/*	(reserver_)(const_)iterator */
	template<typename t_contain>
	class _T_const_iterator
	{
		typedef _T_const_iterator<t_contain> this_type;
	protected:
		typedef t_contain contain_type;
		friend	contain_type;
		friend	typename contain_type::base_type;
		typedef	typename contain_type::value_type	__T_value_type;

		t_contain* _id_bind;							//	容器
		lx_u32 _offset:	type_offset_bits;				//	当前位置偏移
		lx_u32 _tail	:	1;							//	是否是尾部
mutable lx_u32 _init	:	1;							//	是否初始化过下面的内存区

		lx_u8 _value_buf[sizeof __T_value_type];			//	value_type : 惰性计算的一个缓冲区

		_T_const_iterator(contain_type* id_bind_ptr, typename t_contain::type_offset offset, bool is_tail)
			: _id_bind(id_bind_ptr), _offset(offset), _tail(is_tail?1:0), _init(0){}

	public:
		typedef std::bidirectional_iterator_tag		iterator_category;
		typedef typename t_contain::const_pointer	const_pointer;
		typedef typename t_contain::const_pointer	pointer;
		typedef typename t_contain::const_reference	const_reference;
		typedef typename t_contain::const_reference	reference;
		typedef typename t_contain::value_type		value_type;

		_T_const_iterator(void)
			: _id_bind(0)
			{
			}

		bool operator == (const this_type& o)const
			{
			return _id_bind == o._id_bind && _offset == o._offset && _tail == o._tail;
			}

		bool operator != (const this_type& o)const
			{
			return !((*this) == o);
			}

		this_type& operator ++(void)
			{
			_init = 0;
			LOLIX_ASSERT( *this != _id_bind->end(), L"在叠代器尾端继续++");
			_offset = _id_bind->__get_link_by_offset__unsafe__(_offset)->_next;
			if ( _offset == bad_key )
				_tail = 1;

			return *this;
			}
		const this_type operator ++ (int)
			{
			this_type tmp(*this);
			++(*this);
			return tmp;
			}

		this_type& operator --(void)
			{
			_init = 0;
			LOLIX_ASSERT(*this != _id_bind->begin(), L"在叠代器首端继续--");
			this->__goto_prev__();
			return *this;
			}
		const this_type operator -- (int)
			{
			this_type tmp(*this);
			--(*this);
			return tmp;
			}

		const_reference operator* (void)const
			{
			if ( !_init )
				{
				LOLIX_ASSERT(_offset != bad_key, L"非法的反解除指针");
				new ((void*)_value_buf) value_type(__get_key_by_offset__(_offset), *(typename t_contain::mapped_type*)_id_bind->__get_type_buf_by_offset__unsafe__(_offset));
				_init = 1;
				}

			return *reinterpret_cast<const value_type*>(_value_buf);
			}

		const_pointer operator -> (void)const
			{
				return &(**this);
			}
	protected:
		//	给 reverse 叠代器保留的一个接口
		void __goto_prev__(void)
			{
			_init = 0;
			if ( _tail && _offset == bad_key )
				{
				_offset = _id_bind->_usd_back;
				_tail = 0;
				}
			else
				_offset = _id_bind->__get_link_by_offset__unsafe__(_offset)->_prev;
			}
		//	给 reverse 叠代器保留的一个接口
		void __goto_next__(void)
			{
			_init = 0;
			if ( _offset == bad_key )
				{
				if ( !_tail )
					_offset = _id_bind->_usd_front;
				}
			else
				++*this;
			}
	private:
	};

	template< typename t_container>
	class _T_iterator : public t_container::const_iterator
	{
		typedef _T_iterator this_type;
		typedef typename t_container::const_iterator base_type;
		friend	typename base_type::contain_type;
		friend	typename base_type::contain_type::base_type;
		_T_iterator<t_container>(t_container* id_bind_ptr, typename t_container::type_offset offset, bool is_tail)
			: base_type(id_bind_ptr, offset, is_tail){}
	public:
		typedef typename t_container::reference	reference;
		typedef typename t_container::pointer	pointer;

		_T_iterator<t_container>(void){}
		reference operator* (void)const
			{
			return const_cast<reference>(base_type::operator *());
			}

		pointer operator -> (void)const
			{
			return &(**this);
			}
		this_type& operator++ (void)
			{
			base_type::operator++();
			return *this;
			}

		const this_type operator++(int)
			{
			this_type tmp(*this);
			++*this;
			return tmp;
			}

		this_type& operator-- (void)
			{
			base_type::operator--();
			return *this;
			}

		const this_type operator-- (int)
			{
			this_type tmp(*this);
			--*this;
			return tmp;
			}
	protected:
	};


	template<typename t_bidirectional_iterator>
	class _T_reverse_iterator : protected t_bidirectional_iterator
	{
		typedef _T_reverse_iterator<t_bidirectional_iterator>	this_type;
		typedef t_bidirectional_iterator					base_type;

		friend	typename base_type::contain_type;
		friend	typename base_type::contain_type::base_type;
		_T_reverse_iterator(base_type b_type): base_type(b_type)
		{ if ( !_id_bind->empty() ) this->__goto_prev__(); }
	public:
		_T_reverse_iterator(void){}
		this_type& operator++ (void)
			{
			this->__goto_prev__();
			return *this;
			}
		this_type& operator-- (void)
			{
			this->__goto_next__();
			return *this;
			}
		const this_type operator++ (int)
			{
			this_type tmp(*this);
			++*this;
			return tmp;
			}
		const this_type operator-- (int)
			{
			this_type tmp(*this);
			--*this;
			return tmp;
			}

		using base_type::operator*;
		using base_type::operator->;
		bool operator==(this_type const& o)const{return base_type::operator==(o);}
		bool operator!=(this_type const& o)const{return base_type::operator!=(o);}
	};


public:

	size_type capacity(void)const
		{
		return static_cast<size_type>(_key_buf.size()*_e_row_size);
		}
	bool empty(void)const
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		return _size?false:true;
		}

	void reserve(size_type reserve_size)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		if ( this->capacity() >= reserve_size || reserve_size > space_size )
			return;

		this_type::__Reserve_front__unsafe__(reserve_size);
		}

	size_type size(void)const
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		return _size;
		}

	void clear(void)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		static_cast<device_type*>(this)->erase(static_cast<device_type*>(this)->begin(), static_cast<device_type*>(this)->end());
		}
	#pragma	endregion
	typedef _T_const_iterator<device_type>							const_iterator;
	typedef _T_reverse_iterator<const_iterator>						const_reverse_iterator;
	typedef _T_iterator<device_type>								iterator;
	typedef _T_reverse_iterator<iterator>							reverse_iterator;
protected:
	#pragma	region	/*	construct process	*/
	this_type(void)
		: _free_front( (type_offset)bad_key )
		//, _free_back( (type_offset)bad_key )
		, _usd_front( (type_offset)bad_key )
		, _usd_back( (type_offset)bad_key )
		, _size(0)
		, _bind_mode(KEY_BIND_STRATEGY_FAST)
		{ __LOLIX_CHECK_ID_BIND_VALID__(_size); }

	explicit this_type(allocator_type const& alc)
		: _free_front( (type_offset)bad_key )
		//, _free_back( (type_offset)bad_key )
		, _usd_front( (type_offset)bad_key )
		, _usd_back( (type_offset)bad_key )
		, _size(0)
		, _bind_mode(KEY_BIND_STRATEGY_FAST)
		, _key_buf(alc)
		{ __LOLIX_CHECK_ID_BIND_VALID__(_size); }

	explicit this_type(ENUM_KEY_BIND_STRATEGY b_type)
		: _free_front( (type_offset)bad_key )
		//, _free_back( (type_offset)bad_key )
		, _usd_front( (type_offset)bad_key )
		, _usd_back( (type_offset)bad_key )
		, _size(0)
		, _bind_mode(b_type)
		{ __LOLIX_CHECK_ID_BIND_VALID__(_size); }


	template<typename _TA>
	this_type(ENUM_KEY_BIND_STRATEGY b_type, _TA const& alc)
		: _free_front( (type_offset)bad_key )
		//, _free_back( (type_offset)bad_key )
		, _usd_front( (type_offset)bad_key )
		, _usd_back( (type_offset)bad_key )
		, _size(0)
		, _bind_mode(b_type)
		, _key_buf(alc)
		{ __LOLIX_CHECK_ID_BIND_VALID__(_size); }

	this_type(this_type const& o)
		: _free_front(o._free_front)
		, _usd_front(o._usd_front)
		, _usd_back(o._usd_back)
		, _size(o._size)
		, _bind_mode(o._bind_mode)
		, _key_buf(o._key_buf.get_allocator())
		{
		}
	#pragma	endregion
protected:
	key_type	__allocator__(void)
	{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		if ( _size == space_size )
			return key_type::get_bad();
		if ( _size == this->capacity() )
			__Reserve_front__unsafe__(this->capacity() + row_size );
		LOLIX_ASSERT(this->capacity() > size() && _free_front < this->capacity(), L"LOST ALLOC NODE");
		const type_offset rtv = _free_front;
		this_type::__Adjust_free_link__(_bind_mode);
		return __get_key_by_offset__(rtv);
	}

	type_row_cnt __reserve__unsafe__(size_type reserve_size, type_true)
		{
			type_offset capacity = this->capacity();
			const type_row_cnt row_now = static_cast<device_type*>(this)->__reserve__unsafe__(reserve_size, type_false());
			const type_row_cnt row_need = __calc_row_by_size__(reserve_size);

			--capacity;
			--capacity;
			for ( type_row_cnt r = row_now; r < row_need; ++r )
				for ( size_type i = 0; i < row_size; ++i )
					_key_buf[r][i]._prev = ++capacity;

		return row_now;
		}

	//	维护删除时的空闲链
	bool __erase__(key_type k)
		{
		const iterator iter(static_cast<device_type*>(this), k, false);
		iterator next(iter);
		++next;
		__erase__(iter, next);
		return true;
		}

	iterator __erase__(iterator iter_begin, iterator iter_end)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		LOLIX_ASSERT( iter_begin._id_bind == this && iter_end._id_bind == this, L"使用非本地叠代器删除本地对象");
		LOLIX_ASSERT( (iter_begin._offset < this->capacity() || this->empty() && iter_begin._offset == bad_key )&& (iter_end._offset == bad_key || iter_end._offset < this->capacity()), "非法的偏移");

		if ( iter_begin == iter_end )
			return iter_begin;

		const size_type old_size = _size;
		for (type_offset pos = iter_begin._offset ; pos != iter_end._offset; )
			{
			--_size;
			static_cast<device_type*>(this)->__destory_object__for_base__(pos);
			type_link* t_link = this_type::__get_link_by_offset__unsafe__(pos);
			LOLIX_ASSERT(t_link->_usd, L"试图删除非使用段中的内容");
			pos = t_link->_next;
			t_link->_usd = 0;
			}

		__Adjust_free_link_list_during_erase__(old_size, iter_begin._offset, iter_end._offset, iter_end == static_cast<device_type*>(this)->end());
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		return iterator(static_cast<device_type*>(this), _usd_back, bad_key?true:false);
		}

	void __push_link_back__(key_type key)
		{
		type_link* link_key = __get_link_by_offset__unsafe__(__get_offset_by_key__(key));
		LOLIX_ASSERT(!link_key->_usd, L"插入的时候，　对象已经被使用过了");
		link_key->_usd = 1;
		link_key->_prev = _usd_back;
		link_key->_next = (type_offset)bad_key;

		if ( _usd_back != bad_key )
			__get_link_by_offset__unsafe__(_usd_back)->_next = __get_offset_by_key__(key);
		_usd_back = __get_offset_by_key__(key);
		if ( _usd_front == (type_offset)bad_key )
			_usd_front = _usd_back;
		++_size;
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		}

	void __push_link_front__(key_type key)
		{
		type_link* link_key = __get_link_by_offset__unsafe__(__get_offset_by_key__(key));
		LOLIX_ASSERT(!link_key->_usd, L"插入的时候，　对象已经被使用过了");
		link_key->_usd = 1;
		link_key->_prev = (type_offset)bad_key;
		link_key->_next = _usd_front;

		if ( _usd_front != (type_offset)bad_key )
			__get_link_by_offset__unsafe__(_usd_front)->_prev = __get_offset_by_key__(key);
		_usd_front = __get_offset_by_key__(key);
		if ( _usd_back == link_key->_prev )
			_usd_back = _usd_front;

		++_size;
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		}

	type_link* __get_link_by_offset__unsafe__(type_offset offset)
		{
		const type_row_cnt row = static_cast<type_row_cnt>(offset/row_size);
		const type_row_size of = static_cast<type_row_size>(offset%row_size);
		return _key_buf[row]+of;
		}
	const type_link* __get_link_by_offset__unsafe__(type_offset offset)const
		{
		return const_cast<this_type*>(this)->__get_link_by_offset__unsafe__(offset);
		}

	bool	__get_link_by_offset__safe__(type_link** out_ptr, type_offset offset)
		{
		const type_row_cnt row = static_cast<type_row_cnt>(offset/row_size);
		if ( row >= _key_buf.size() )
			return false;

		const type_row_size of = static_cast<type_row_size>(offset%row_size);
		*out_ptr = _key_buf[row]+of;
		return true;
		}
	bool	__get_link_by_offset__safe__(const type_link** out_ptr, type_offset offset)const
		{
		return const_cast<this_type*>(this)->__get_link_by_offset__safe__(const_cast<type_link**>(out_ptr), offset);
		}
	//
	type_row_cnt __calc_row_by_size__(size_type id_size)const
		{
		return static_cast<type_row_cnt>((id_size+row_size-1)/row_size);
		}


	type_offset __make_offset_by_pos__(type_row_cnt r, type_row_size o)const
		{
		return r*row_size + o;
		}

	void __swap(this_type& o)
		{
		_key_buf.swap(o._key_buf);
#define	__LOLIX_TMP__SWAP__(x)	lolix::swap(x, o.x)
		__LOLIX_TMP__SWAP__(_free_front);
		//__LOLIX_TMP__SWAP__(_free_back);
		__LOLIX_TMP__SWAP__(_usd_front);
		__LOLIX_TMP__SWAP__(_usd_back);
		__LOLIX_TMP__SWAP__(_size);
#undef	__LOLIX_TMP__SWAP__
		}


#if			LOLIX_CFG__IS_DEBUG
	void __LOLIX_CHECK_ID_BIND_VALID__(type_offset true_size)const
		{
		if ( !this->capacity() )
			return;
		if ( this->capacity() != _size )
			{
			LOLIX_ASSERT(_free_front != bad_key, L"空闲节点头节点指向错误");
			__LOLIX_CHECK_ID_BIND_VALID__(true_size, typename device_type::adjust_prev_flag());
			}
		else
			{
				LOLIX_ASSERT(_free_front == bad_key, L"空闲节点头节点指向错误");
			}
		}

	void __LOLIX_CHECK_ID_BIND_VALID__(type_offset true_size, lolix::type_false)const
		{
		const type_link* lk_f = __get_link_by_offset__unsafe__(_free_front);
		LOLIX_ASSERT(!lk_f->_usd, L"空闲节点头使用不空");
		if ( _bind_mode != KEY_BIND_STRATEGY_FAST )
			{
			LOLIX_ASSERT(lk_f->_prev != bad_key, L"空闲节点头使用不空");
			const type_link* lk_b = __get_link_by_offset__unsafe__(lk_f->_prev);
			LOLIX_ASSERT(!lk_b->_usd && lk_b->_next == bad_key, L"空闲尾节点指向错误");
			if ( true_size == this->capacity() - 1 )
				{
				LOLIX_ASSERT(lk_f->_prev == _free_front, L"空闲头前向指向非自身");
				}
			else
				{
				LOLIX_ASSERT(lk_f->_prev != _free_front, L"空闲头前向指向自身");
				}
			}
		}


	void __LOLIX_CHECK_ID_BIND_VALID__(type_offset true_size, lolix::type_true)const
		{
		const type_link* lk_f = __get_link_by_offset__unsafe__(_free_front);
		LOLIX_ASSERT(!lk_f->_usd, L"空闲节点头使用不空");
		LOLIX_ASSERT(lk_f->_prev != bad_key, L"空闲节点头使用不空");
		const type_link* lk_b = __get_link_by_offset__unsafe__(lk_f->_prev);
		LOLIX_ASSERT(!lk_b->_usd && lk_b->_next == bad_key, L"空闲尾节点指向错误");
		if ( true_size == this->capacity() - 1 )
			{
			LOLIX_ASSERT(lk_f->_prev == _free_front, L"空闲头前向指向非自身");
			}
		else
			{
			LOLIX_ASSERT(lk_f->_prev != _free_front, L"空闲头前向指向自身");
			}
		}


#else	//	LOLIX_CFG__IS_DEBUG
	LOLIX_INLINE_CALL	void __LOLIX_CHECK_ID_BIND_VALID__(type_offset)const{}

#endif	//	LOLIX_CFG__IS_DEBUG

	bool __is_need_adjust_free_list_prev_link(void)const
	{
		if ( lolix::type::type_equal<typename device_type::adjust_prev_flag, lolix::type_true>::value || _bind_mode != KEY_BIND_STRATEGY_FAST )
			return true;
		return false;
	}
protected:
	type_offset _free_front;

	type_offset _usd_front;
	type_offset _usd_back;

	type_offset	_size;

	const ENUM_KEY_BIND_STRATEGY	_bind_mode;

	vector<type_link*, allocator_type>	_key_buf;

private:
	void __Reserve_front__unsafe__(size_type reserve_size)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		type_row_cnt row_old = static_cast<device_type*>(this)->__reserve__unsafe__(reserve_size, typename device_type::adjust_prev_flag());
		__Make_reserve_buf_push_front__(row_old, typename device_type::adjust_prev_flag());
		}

	void __Adjust_free_link__(ENUM_KEY_BIND_STRATEGY b_mode)
		{
		LOLIX_ASSERT(!this_type::__get_link_by_offset__unsafe__(_free_front)->_usd, L"空闲节点头使用不空");
		LOLIX_ASSERT(this_type::__get_link_by_offset__unsafe__(_free_front)->_next != _free_front || this->capacity() - this->_size == 1, L"节点定位重复");

		const type_offset old_free = _free_front;
		const type_link* const old_lk = this_type::__get_link_by_offset__unsafe__(_free_front);
		_free_front = old_lk->_next;

		if ( (b_mode != KEY_BIND_STRATEGY_FAST || __is_need_adjust_free_list_prev_link()) && _free_front != bad_key )
			{
			type_link* const cur_lk = this_type::__get_link_by_offset__unsafe__(_free_front);
			cur_lk->_prev = old_lk->_prev;
			}

		if ( b_mode == KEY_BIND_STRATEGY_SAFE && _free_front < old_free && this->capacity() != space_size )
			{
			static_cast<device_type*>(this)->__Reserve_front__unsafe__(this->capacity() + row_size);
			LOLIX_ASSERT( _free_front > old_free, L"非法的容器状态被捕获, 在容器空间被全部利用前, 空闲头节点试图回绕" );
			}
		}

	void __Adjust_free_link_list_during_erase__(size_type old_size, type_offset off_begin, type_offset off_end, bool off_end_is_tail)
		{
		//	求删除后的 back 位置
		type_link* const t_begin = this_type::__get_link_by_offset__unsafe__(off_begin);
		LOLIX_ASSERT( t_begin->_prev == bad_key || this_type::__get_link_by_offset__unsafe__(t_begin->_prev)->_usd, L"从分配堆中找出一个没有被分配的对象");

		type_link* t_end;

		type_offset off_back;
		if ( !off_end_is_tail )
			{	//	状态 ??XXXX---
			t_end = this_type::__get_link_by_offset__unsafe__(off_end);
			LOLIX_ASSERT(t_end->_usd, L"从分配堆?找出一个没有被分配的对象");
			off_back = t_end->_prev;
			t_end->_prev = t_begin->_prev;

			if ( _usd_front == off_begin )	//	XXXX----
				_usd_front = off_end;
			else	//	---XXXX----
				this_type::__get_link_by_offset__unsafe__(t_begin->_prev)->_next = off_end;
			}
		else
			{	//	????XXXXX
			off_back = _usd_back;
			_usd_back = t_begin->_prev;
			if ( _usd_back == bad_key )	//	前面的也挂掉了	//	XXXXXXXX
				_usd_front = bad_key;
			else	//	--XXXXXX
				this_type::__get_link_by_offset__unsafe__(t_begin->_prev)->_next = bad_key;
			}

		if ( _bind_mode == KEY_BIND_STRATEGY_FAST )
			{
			LOLIX_ASSERT(this->capacity() != old_size || _free_front == bad_key, L"全满分配器里面的 _free_front 依旧有效");
			type_link* const lk_back = __get_link_by_offset__unsafe__(off_back);
			if ( this->capacity() != old_size )
				{
				if ( __is_need_adjust_free_list_prev_link() )
					{
					type_link* const lk_front = __get_link_by_offset__unsafe__(_free_front);
					t_begin->_prev = lk_front->_prev;
					lk_front->_prev = off_back;
					}
				}

			lk_back->_next = _free_front;
			_free_front = off_begin;
			}
		else
			{
			type_link* free_lk;
			if ( this->capacity() == old_size )
				{	//	如果为非空闲空间容器， 则直接调整 _free_front 地址
				LOLIX_ASSERT(_free_front == bad_key, L"全满分配器里面的 _free_front 依旧有效");
				_free_front = off_begin;
				free_lk = this_type::__get_link_by_offset__unsafe__(_free_front);
				}
			else
				{
				LOLIX_ASSERT_VOID(off_begin != bad_key);
				free_lk = this_type::__get_link_by_offset__unsafe__(_free_front);
				if ( __is_need_adjust_free_list_prev_link() )
					t_begin->_prev = free_lk->_prev;
				this_type::__get_link_by_offset__unsafe__(free_lk->_prev)->_next = off_begin;
				}
			LOLIX_ASSERT_VOID(off_back != bad_key);
			free_lk->_prev = off_back;
			this_type::__get_link_by_offset__unsafe__(off_back)->_next = bad_key;
			//_free_back = off_back;
			}

		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		}

	//	make the allocated memory add to free memory front;
	void __Make_reserve_buf_push_front__(type_row_cnt row_old, type_false)
		{
		const type_row_cnt row_now = static_cast<type_row_cnt>(_key_buf.size());
		LOLIX_ASSERT(row_old < row_now, L"新分配大小小于已经存在大小，错误啦");
		const type_offset old_free_front = _free_front;
		_key_buf[row_now-1][row_size-1]._next = _free_front;
		_free_front = this_type::__make_offset_by_pos__(row_old, 0);

		if ( _bind_mode != KEY_BIND_STRATEGY_FAST )
			{
			type_link* fr_lk = __get_link_by_offset__unsafe__(_free_front);
			if ( old_free_front != bad_key )
				{
				const type_link* tplk = __get_link_by_offset__unsafe__(old_free_front);
				fr_lk->_prev = tplk->_prev;
				}
			else
				{
				fr_lk->_prev = this_type::__make_offset_by_pos__(row_now-1, row_size-1);
				}
			LOLIX_ASSERT( __get_link_by_offset__unsafe__(fr_lk->_prev)->_next == bad_key, L"新分配的时候末尾节点后续不是无效节点");
			}
		}

	void __Make_reserve_buf_push_front__(type_row_cnt row_old, type_true)
		{
		const type_row_cnt row_now = static_cast<type_row_cnt>(_key_buf.size());
		const type_offset old_free_front = _free_front;
		__Make_reserve_buf_push_front__(row_old, type_false());
		if ( _bind_mode == KEY_BIND_STRATEGY_FAST )
			{
			type_link* fr_lk = __get_link_by_offset__unsafe__(_free_front);
			if ( old_free_front != bad_key )
				{
				type_link* tplk = __get_link_by_offset__unsafe__(old_free_front);
				fr_lk->_prev = tplk->_prev;
				tplk->_prev = this_type::__make_offset_by_pos__(row_now-1, row_size-1);
				}
			else
				{
				fr_lk->_prev = this_type::__make_offset_by_pos__(row_now-1, row_size-1);
				}
			}
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		}
};


template<typename bind_type, lolix::size_type max_element_size, lolix::size_type row_size_input, typename _T_ALLOC, typename need_adjust_free_prev_pointer>
class lolix::toy::key_bind
	: protected _help::key_bind_base<max_element_size, row_size_input, _T_ALLOC, key_bind<bind_type, max_element_size, row_size_input, _T_ALLOC, need_adjust_free_prev_pointer> >
{
	friend _help::key_bind_archive;
	CANNT_COPY_LOLI;
protected:
	typedef key_bind<bind_type, max_element_size, row_size_input, _T_ALLOC, need_adjust_free_prev_pointer> this_type;
	typedef _help::key_bind_base<max_element_size, row_size_input, _T_ALLOC, this_type> base_type;
	friend base_type;
	typedef typename base_type::allocator_type	_ALLOC_TYPE;
	typedef	need_adjust_free_prev_pointer		adjust_prev_flag;

	typedef  pair<const key_type, bind_type&> _T_value;

	typedef	typename _ALLOC_TYPE::rebind<bind_type>::other	_ALLOC_BIND;
	typedef typename _ALLOC_TYPE::rebind<type_link>::other	_ALLOC_LINK;
public:
	typedef _ALLOC_TYPE	allocator_type;

	using base_type::bad_key;
	using base_type::key_type_bits;
	using base_type::row_cnt;
	using base_type::row_size;
	using base_type::space_size;

	using base_type::capacity;
	using base_type::clear;
	using base_type::empty;
	using base_type::mode;
	using base_type::reserve;
	using base_type::size;

	typedef typename base_type::key_type	key_type;
	typedef typename base_type::size_type	size_type;
	typedef typename base_type::const_iterator						const_iterator;
	typedef typename type::pass_type<bind_type>::const_value_type	const_pass_type;
	typedef _T_value const*											const_pointer;
	typedef _T_value const&											const_reference;
	typedef typename base_type::const_reverse_iterator				const_reverse_iterator;
	typedef typename base_type::iterator							iterator;
	typedef bind_type												mapped_type;
	typedef _T_value*												pointer;
	typedef _T_value&												reference;
	typedef bind_type												referent_type;
	typedef typename base_type::reverse_iterator					reverse_iterator;
	typedef	_T_value												value_type;

	friend const_iterator;
	friend const_reverse_iterator;
	friend iterator;
	friend reverse_iterator;


	this_type(void){}

	explicit this_type(const allocator_type& alc)
		: base_type( alc )
		, _alloc_bind(alc)
		, _alloc_link(alc)
		, _type_buf(alc)
		{ }

	explicit this_type(ENUM_KEY_BIND_STRATEGY b_type): base_type(b_type){ }
	this_type(ENUM_KEY_BIND_STRATEGY b_type, const allocator_type& alc)
		: base_type(b_type, alc)
		, _alloc_bind(alc)
		, _alloc_link(alc)
		, _type_buf(alc)
		{ }

	this_type(const this_type& o)
		: base_type(o)
		, _alloc_bind(o._alloc_bind)
		, _alloc_link(o._alloc_link)
		, _type_buf(o._type_buf.get_allocator())
		{
		//device_type* dev = (device_type*)this;
		_key_buf.reserve(o._key_buf.size());
		_type_buf.reserve(o._type_buf.size());

		for ( size_type i = 0; i != o._key_buf.size(); ++i )
			{
			_key_buf.push_back(_alloc_link.allocate(row_size));
			_type_buf.push_back(_alloc_bind.allocate(row_size));
			for ( size_type k = 0; k != row_size; ++k )
				{
				_key_buf[i][k]._prev = o._key_buf[i][k]._prev;
				_key_buf[i][k]._next = o._key_buf[i][k]._next;
				_key_buf[i][k]._usd = o._key_buf[i][k]._usd;
				if ( _key_buf[i][k]._usd )
					_alloc_bind.construct(&_type_buf[i][k], o._type_buf[i][k]);
				}
			}
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		}

	~key_bind<bind_type, max_element_size, row_size_input, _T_ALLOC, need_adjust_free_prev_pointer>(void)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		clear();
		type_row_cnt rnow = static_cast<type_row_cnt>(_key_buf.size());
		for ( type_row_cnt i = 0; i != rnow; ++i )
			{
			this->_alloc_link.deallocate(_key_buf[i], row_size);
			this->_alloc_bind.deallocate(_type_buf[i], row_size);
			}
		}


	iterator begin(void){return iterator(this, _usd_front, this->empty());}
	const_iterator begin(void)const{return const_cast<this_type&>(*this).begin();}

	iterator end(void){return iterator(this, bad_key, 1);}
	const_iterator end(void)const{return const_cast<this_type&>(*this).end();}

	iterator erase(iterator iter)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		iterator iter_end = iter;
		++iter_end;
		return erase(iter, iter_end);
		}

	iterator erase(iterator iter_begin, iterator iter_end)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		LOLIX_ASSERT(iter_begin._id_bind == this && iter_end._id_bind == this, L"使用非本地叠代器删除本地对象");
		LOLIX_ASSERT( (iter_begin._offset < this->capacity() || this->empty() && iter_begin._offset == bad_key )&& (iter_end._offset == bad_key || iter_end._offset < this->capacity()), L"非法的偏移");

		if ( iter_begin == iter_end )
			return iter_begin;
		return __erase__(iter_begin, iter_end);
		}

	size_type erase(key_type k)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		base_type::__erase__(k);
		return 1;
		}


	const_iterator find(key_type key)const{return const_cast<this_type*>(this)->find(key);}
	iterator find(key_type key)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		type_link* ty_lk;
		if ( key.is_valid() && this->__get_link_by_offset__safe__(&ty_lk, __get_offset_by_key__(key)) && ty_lk->_usd )
			return iterator(this, __get_offset_by_key__(key), 0);
		return this->end();
		}

	mapped_type const* get(key_type k)const{return const_cast<this_type*>(this)->get(k);}
	mapped_type* get(key_type k)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		type_link* ty_lk;
		if ( !this->__get_link_by_offset__safe__(&ty_lk, __get_offset_by_key__(k)) || !ty_lk->_usd )
			return 0;
		return (bind_type*)this_type::__get_type_buf_by_offset__unsafe__(__get_offset_by_key__(k));
		}

	key_type push_back(const_pass_type value)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		key_type key = this_type::__try_reserve_and_create_if_need_befor_push__(value);
		if ( !key.is_bad() )
			__push_link_back__(key);
		return key;
		}

	key_type push_front(const_pass_type value)
		{
		key_type key = this_type::__try_reserve_and_create_if_need_befor_push__(value);
		if ( !key.is_bad() )
			__push_link_front__(key);
		return key;
		}

	const_reverse_iterator rbegin(void)const{return const_reverse_iterator(this->end());}
	reverse_iterator rbegin(void){return reverse_iterator(this->end());}
	const_reverse_iterator rend(void)const{return const_reverse_iterator(this->begin());}
	reverse_iterator rend(void){return reverse_iterator(this->begin());}

	void swap(this_type& o)
		{
		__swap(o);
		_type_buf.swap(o._type_buf);
		}

	const_pass_type operator [](key_type k)const
		{
		LOLIX_ASSERT(get(k), L"非法的ID");
		return *this->__get_type_buf_by_offset__unsafe__(__get_offset_by_key__(k));
		}
	mapped_type& operator[](key_type k)
		{
		LOLIX_ASSERT(get(k), L"非法的ID");
		return *this->__get_type_buf_by_offset__unsafe__(__get_offset_by_key__(k));
		}

	bool operator == (this_type const& o)const
		{
		if ( o.size() != this->size() )
			return false;

		for ( const_iterator iter = begin(), iter_end = end(), oiter = o.begin(); iter != iter_end; ++iter, ++oiter )
			if ( *iter != *oiter )
				return false;

		return true;
		}

	bool operator != (this_type const& o)const
		{
		return !((*this) == o);
		}

	this_type& operator = (const this_type& o)
		{
		this_type tmp(o);
		this->swap(tmp);
		return *this;
		}

protected:
	bind_type* __get_type_buf_by_offset__unsafe__(type_offset offset)
		{
		type_row_cnt row = static_cast<type_row_cnt>(offset/row_size);
		type_row_size of = static_cast<type_row_size>(offset%row_size);
		return _type_buf[row]+of;
		}
	const bind_type* __get_type_buf_by_offset__unsafe__(type_offset offset)const
		{
		type_row_cnt row = static_cast<type_row_cnt>(offset/row_size);
		type_row_size of = static_cast<type_row_size>(offset%row_size);
		return _type_buf[row]+of;
		}


	using base_type::__reserve__unsafe__;
	//	allocate memory
	type_row_cnt __reserve__unsafe__(size_type reserve_size, type_false)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		type_offset capacity = this->capacity();
		const type_row_cnt row_now = static_cast<type_row_cnt>(_key_buf.size());
		const type_row_cnt row_need = __calc_row_by_size__(reserve_size);

		_key_buf.reserve(row_need);
		_type_buf.reserve(row_need);
		for ( type_row_cnt r = row_now; r < row_need; ++r )
			{
			_key_buf.push_back(this->_alloc_link.allocate(row_size));
			_type_buf.push_back(this->_alloc_bind.allocate(row_size));

			for ( size_type i = 0; i < row_size; ++i )
				{
				_key_buf[r][i]._usd = 0;
				_key_buf[r][i]._next = ++capacity;
				}
			}

		return row_now;
		}

	key_type __try_reserve_and_create_if_need_befor_push__(const_pass_type value)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		key_type new_key = __allocator__();
		if ( new_key.is_bad() )
			return new_key;

		bind_type* ptr = this_type::__get_type_buf_by_offset__unsafe__(__get_offset_by_key__(new_key));
		this->_alloc_bind.construct(ptr, bind_type(value));
		return new_key;
		}

	void	__destory_object__for_base__(type_offset pos)
		{
		this->_alloc_bind.destroy(this_type::__get_type_buf_by_offset__unsafe__(pos));
		}

	_ALLOC_BIND	_alloc_bind;
	_ALLOC_LINK	_alloc_link;
	vector<bind_type*, allocator_type>	_type_buf;
};





template<lolix::size_type max_element_size, lolix::size_type row_size_input, typename _T_ALC, typename need_adjust_free_prev_pointer>
class lolix::toy::key_bind<void, max_element_size, row_size_input, _T_ALC, need_adjust_free_prev_pointer>
	: protected _help::key_bind_base<max_element_size, row_size_input, _T_ALC, key_bind<void, max_element_size, row_size_input, _T_ALC, need_adjust_free_prev_pointer> >
{
	CANNT_COPY_LOLI;
	friend _help::key_bind_archive;
protected:
	typedef key_bind<void, max_element_size, row_size_input, _T_ALC, need_adjust_free_prev_pointer> this_type;
	typedef _help::key_bind_base<max_element_size, row_size_input, _T_ALC, this_type> base_type;
	friend base_type;
	typedef  pair<const key_type, void> _T_value;
	typedef	need_adjust_free_prev_pointer		adjust_prev_flag;

public:
	typedef	_T_ALC	allocator_type;

	using base_type::bad_key;
	using base_type::key_type;
	using base_type::key_type_bits;
	using base_type::row_cnt;
	using base_type::row_size;
	using base_type::size_type;
	using base_type::space_size;

	using base_type::capacity;
	using base_type::empty;
	using base_type::mode;
	using base_type::size;

	typedef _T_const_iterator<this_type>		const_iterator;
	typedef _T_value const*						const_pointer;
	typedef _T_value const&						const_reference;
	typedef _T_reverse_iterator<const_iterator>	const_reverse_iterator;
	typedef _T_iterator<this_type>				iterator;
	typedef void								mapped_type;
	typedef _T_value*							pointer;
	typedef _T_value&							reference;
	typedef void								referent_type;
	typedef _T_reverse_iterator<iterator>		reverse_iterator;
	typedef _T_value							value_type;

	friend const_iterator;
	friend const_reverse_iterator;
	friend iterator;
	friend reverse_iterator;

	LOLIX_INLINE_CALL this_type(void)		{ }
	LOLIX_INLINE_CALL this_type(const this_type& o): base_type(o){}

	LOLIX_INLINE_CALL explicit this_type(ENUM_KEY_BIND_STRATEGY b_type): base_type(b_type)	{ }
	LOLIX_INLINE_CALL explicit this_type(const allocator_type& alc)		: base_type(alc){}
	LOLIX_INLINE_CALL this_type(ENUM_KEY_BIND_STRATEGY b_type, const allocator_type& alc)
		: base_type(b_type)
		, _alloc_bind(alc)
		, _alloc_link(alc)
		{ }

	LOLIX_INLINE_CALL ~key_bind<void, max_element_size, row_size_input, _T_ALC, need_adjust_free_prev_pointer>(void)
		{
		clear();
		type_row_cnt rnow = static_cast<type_row_cnt>(_key_buf.size());
		for ( type_row_cnt i = 0; i != rnow; ++i )
			this->_alloc_link.deallocate(_key_buf[i], row_size);
		}

	LOLIX_INLINE_CALL const_iterator begin(void)const{return const_cast<this_type&>(*this).begin();}
	LOLIX_INLINE_CALL iterator begin(void){return iterator(this, _usd_front, this->empty());}

	LOLIX_INLINE_CALL const_iterator end(void)const{return const_cast<this_type&>(*this).end();}
	LOLIX_INLINE_CALL iterator end(void){return iterator(this, bad_key, 1);}

	LOLIX_INLINE_CALL iterator erase(iterator iter)
		{
		__LOLIX_CHECK_ID_BIND_VALID__();
		iterator iter_end = iter;
		++iter_end;
		return erase(iter, iter_end);
		}

	LOLIX_INLINE_CALL iterator erase(iterator iter_begin, iterator iter_end)
		{
		return __erase__(iter_begin, iter_end);
		}

	LOLIX_INLINE_CALL size_type erase(key_type k){ base_type::__erase__(k); return 1;}
	LOLIX_INLINE_CALL key_type push_back(void)
		{
		__LOLIX_CHECK_ID_BIND_VALID__();
		key_type key = this_type::__try_reserve_and_create_if_need_befor_push__();
		if ( !key.is_bad() )
			__push_link_back__(key);
		return key;
		}

	LOLIX_INLINE_CALL key_type push_front(void)
		{
		key_type key = this_type::__try_reserve_and_create_if_need_befor_push__();
		if ( !key.is_bad() )
			__push_link_front__(key);
		return key;
		}

	LOLIX_INLINE_CALL const_reverse_iterator rbegin(void)const{return const_reverse_iterator(this->end());}
	LOLIX_INLINE_CALL reverse_iterator rbegin(void){return reverse_iterator(this->end());}
	LOLIX_INLINE_CALL reverse_iterator rend(void){return reverse_iterator(this->begin());}
	LOLIX_INLINE_CALL const_reverse_iterator rend(void)const{return const_reverse_iterator(this->begin());}

	LOLIX_INLINE_CALL void swap(this_type& o)
		{
		__swap(o);
		}

	LOLIX_INLINE_CALL bool operator [](key_type k)const
		{
		__LOLIX_CHECK_ID_BIND_VALID__();
		if ( k >= this->capacity() )
			return false;
		const type_link* link = this_type::__get_link_by_offset__unsafe__(k);
		if ( link->_usd )
			return true;
		return false;
		}

	LOLIX_INLINE_CALL bool operator == (this_type const& o)const
		{
			if ( o.size() != this->size() )
				return false;

			for ( const_iterator iter = begin(), iter_end = end(), oiter = o.begin(); iter != iter_end; ++iter )
				if ( *iter != *oiter )
					return false;
		}

	LOLIX_INLINE_CALL bool operator != (this_type const& o)const
		{
		return !((*this) == o);
		}

	LOLIX_INLINE_CALL this_type& operator = (this_type const& o)
		{
		base_type::operator =(o);
		return *this;
		}


protected:
	using base_type::__reserve__unsafe__;
	//	allocate memory
	LOLIX_INLINE_CALL type_row_cnt __reserve__unsafe__(size_type reserve_size, type_false)
		{
		__LOLIX_CHECK_ID_BIND_VALID__();
		type_offset capacity = this->capacity();
		const type_row_cnt row_now = static_cast<type_row_cnt>(_key_buf.size());
		const type_row_cnt row_need = this_type::__calc_row_by_size__(reserve_size);

		_key_buf.resize(row_need);
		for ( type_row_cnt r = row_now; r < row_need; ++r )
			{
			_key_buf[r] = this->_alloc_link.allocate(row_size);
			for ( size_type i = 0; i < row_size; ++i )
				{
				_key_buf[r][i]._usd = 0;
				_key_buf[r][i]._next = ++capacity;
				}
			}

		return row_now;
		}

	LOLIX_INLINE_CALL key_type __try_reserve_and_create_if_need_befor_push__(void)
		{
		return __allocator__();
		}

	LOLIX_INLINE_CALL void	__destory_object__for_base__(type_offset /*pos*/)
		{
		}
};


template <typename bind_type, lolix::size_type max_element_size, lolix::size_type row_size_input, typename _T_ALLOC>
class lolix::toy::key_bind_random
	: protected key_bind<bind_type, max_element_size, row_size_input, _T_ALLOC, type_true>
{
	CANNT_COPY_LOLI;
	friend	_help::key_bind_archive;
	typedef	key_bind<bind_type, max_element_size, row_size_input, _T_ALLOC, type_true>	base_type;
	typedef	key_bind_random<bind_type, max_element_size, row_size_input, _T_ALLOC>	this_type;
public:
	using base_type::allocator_type;
	using base_type::key_type;
	using base_type::key_type_bits;
	using base_type::row_cnt;
	using base_type::row_size;
	using base_type::size_type;
	using base_type::space_size;

	using base_type::capacity;
	using base_type::empty;
	using base_type::mode;
	using base_type::reserve;
	using base_type::size;

	using base_type::begin;
	using base_type::end;
	using base_type::clear;
	using base_type::erase;
	using base_type::find;
	using base_type::get;
	using base_type::push_back;
	using base_type::push_front;
	using base_type::rbegin;
	using base_type::rend;
	using base_type::operator [];
	using base_type::operator ==;
	using base_type::operator !=;

	using base_type::const_iterator;
	using base_type::const_pass_type;
	using base_type::const_pointer;
	using base_type::const_reference;
	using base_type::const_reverse_iterator;
	using base_type::iterator;
	using base_type::mapped_type;
	using base_type::pointer;
	using base_type::reference;
	using base_type::referent_type;
	using base_type::reverse_iterator;
	using base_type::value_type;

	LOLIX_INLINE_CALL this_type(void){}
	LOLIX_INLINE_CALL this_type(const this_type& o): base_type(o){}
	LOLIX_INLINE_CALL explicit this_type(const allocator_type& alc): base_type(alc){ }
	LOLIX_INLINE_CALL explicit this_type(ENUM_KEY_BIND_STRATEGY b_type): base_type(b_type){}
	LOLIX_INLINE_CALL this_type(ENUM_KEY_BIND_STRATEGY b_type, const allocator_type& alc): base_type(b_type, alc){}

	LOLIX_INLINE_CALL void swap(this_type& o)
		{
		base_type::swap(o);
		}

	template<typename _PassValue>
	LOLIX_INLINE_CALL mapped_type& set_value_by_key(key_type k, _PassValue const& val)
		{
		if ( __push_back_by_gived_key(k) )
			return *new (__get_type_buf_by_offset__unsafe__(k._key)) mapped_type(val);

		return *get(k);
		}

	LOLIX_INLINE_CALL this_type& operator = (const this_type& o)
		{
		base_type::operator = (o);
		return *this;
		}

protected:
	typedef	typename base_type::adjust_prev_flag	adjust_prev_flag;
	//	标记指定的　key　为使用状态　: 进行过必要的链表重组，　但是没有初始化变量
	LOLIX_INLINE_CALL bool __push_back_by_gived_key(key_type k)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		type_link* link_key = __alloc_gived_key(k);
		if ( !link_key )
			return false;

		++_size;
		LOLIX_ASSERT(_usd_back != bad_key || _usd_front == bad_key && _usd_back == bad_key, L"后使用节点为非法，但是前使用指向有效！错误");

		if ( _usd_back == bad_key )
			_usd_front = k._key;
		else
			this_type::__get_link_by_offset__unsafe__(_usd_back)->_next = k._key;

		link_key->_prev = _usd_back;
		link_key->_next = bad_key;
		link_key->_usd = 1;
		_usd_back = k._key;
		__LOLIX_CHECK_ID_BIND_VALID__(_size);

#if			LOLIX_CFG__IS_DEBUG
		for ( type_offset i = _free_front; i != bad_key; )
			{
			const type_link* cf = __get_link_by_offset__unsafe__(i);
			LOLIX_ASSERT(cf != link_key, L"空闲列表存在正在被使用的指针");
			i = cf->_next;
			}
#endif	//	LOLIX_CFG__IS_DEBUG
		return true;
		}

	LOLIX_INLINE_CALL type_link* __alloc_gived_key(const key_type k)
		{
		__LOLIX_CHECK_ID_BIND_VALID__(_size);
		//	分配好了空间
#if			LOLIX_CFG__IS_DEBUG
		do
			{
			type_link const* tlk;
			if ( !__get_link_by_offset__safe__(&tlk, k._key) )
				break;
		}while(0);

		//std::vector<type_offset, allocator_type> vec(_key_buf.get_allocator());
		for ( type_offset i = _free_front; i != bad_key; )
			{
			//vec.push_back(i);
			const type_link* cf = __get_link_by_offset__unsafe__(i);
			LOLIX_ASSERT(cf->_prev != bad_key || i == _free_front, L"空闲列表前项没有被正确维护");
			i = cf->_next;
			}
		//std::sort(vec.begin(), vec.end());
		//std::vector<type_offset, allocator_type>::iterator iter = std::unique(vec.begin(), vec.end());
		//LOLIX_ASSERT(iter == vec.end(), L"存在重复的空闲节点");
#endif	//	LOLIX_CFG__IS_DEBUG

		reserve(k._key + 1);
#if			LOLIX_CFG__IS_DEBUG
		do
			{
			__LOLIX_CHECK_ID_BIND_VALID__(_size);
			type_link* new_front = this_type::__get_link_by_offset__unsafe__(_free_front);
			type_link* new_tail = this_type::__get_link_by_offset__unsafe__(new_front->_prev);
			LOLIX_ASSERT(new_tail->_next == bad_key, L"空闲列表尾部无效");
			//vec.clear();
			for ( type_offset i = _free_front; i != bad_key; )
				{
				//vec.push_back(i);
				const type_link* cf = __get_link_by_offset__unsafe__(i);
				LOLIX_ASSERT(cf->_prev != bad_key || i == _free_front, L"空闲列表前项没有被正确维护");
				i = cf->_next;
				}
			//std::sort(vec.begin(), vec.end());
			//iter = std::unique(vec.begin(), vec.end());
			//LOLIX_ASSERT(iter == vec.end(), L"存在重复的空闲节点");
			}
		while(0);

#endif	//	LOLIX_CFG__IS_DEBUG

		type_link* link_key = this_type::__get_link_by_offset__unsafe__(k._key);
		if ( link_key->_usd )
			return 0;

#if			LOLIX_CFG__IS_DEBUG
		LOLIX_ASSERT(link_key->_prev != bad_key, L"采样节点的前向节点无效");
		const type_link* cur_next = this_type::__get_link_by_offset__unsafe__(link_key->_prev);
		LOLIX_ASSERT(cur_next->_next == bad_key || cur_next->_next == k._key, L"空闲节点指向错误");
#endif	//	LOLIX_CFG__IS_DEBUG

		if ( /*_bind_mode != KEY_BIND_STRATEGY_FAST && */link_key->_next != bad_key )
			this_type::__get_link_by_offset__unsafe__(link_key->_next)->_prev = link_key->_prev;
		else
			this_type::__get_link_by_offset__unsafe__(_free_front)->_prev = link_key->_prev;

		if ( _free_front == k._key )
			_free_front = link_key->_next;
		else
			this_type::__get_link_by_offset__unsafe__(link_key->_prev)->_next = link_key->_next;

#if			LOLIX_CFG__IS_DEBUG
			{
			__LOLIX_CHECK_ID_BIND_VALID__(_size+1);
			type_link* new_front = this_type::__get_link_by_offset__unsafe__(_free_front);
			type_link* new_tail = this_type::__get_link_by_offset__unsafe__(new_front->_prev);
			LOLIX_ASSERT(new_tail->_next == bad_key, L"空闲列表尾部无效");

			//vec.clear();
			for ( type_offset i = _free_front; i != bad_key; )
				{
				//vec.push_back(i);
				const type_link* cf = __get_link_by_offset__unsafe__(i);
				LOLIX_ASSERT(cf->_prev != bad_key || i == _free_front, L"空闲列表前项没有被正确维护");
				LOLIX_ASSERT(cf != link_key, L"空闲列表存在正在被使用的指针");
				i = cf->_next;
				}
			//std::sort(vec.begin(), vec.end());
			//iter = std::unique(vec.begin(), vec.end());
			//LOLIX_ASSERT(iter == vec.end(), L"存在重复的空闲节点");
			}
#endif	//	LOLIX_CFG__IS_DEBUG
		return link_key;
		}

};



class LOLIX_NO_VTABLE lolix::toy::key_bind_dynamic_handle
	: public lolix::itf
{
public:
	typedef	lx_u64	key_type;
	LOLIX_DECLARE_ITF(lolix::toy::key_bind_dynamic_handle, lolix::itf, (0xae73da4c, 0x1e354b34, 0x9a25b680, 0x3cfc0c4f));
	LOLIX_DEFINE_FACTORY((0xf1f47415, 0xd7344798, 0x80023a33, 0xb4a3c367) , (key_bind_dynamic_handle** out_ptr, key_type block_size, key_type inc_size, key_type bind_strategy/* = KEY_BIND_STRATEGY_FAST*/));


	//--------------------------------------------------------------------------
	//	>> 2007-03-30 01:08:36 >>------------------->> 2007-03-30 01:08:36 >>
		//FUNC>>$	:	得到该 ID 绑定器的最大支持大小
			//FUNC>>@	:	[null]
				//FUNC>>#	:	返回特定的大小（任何一个绑定器不能绑定数量为空）
	//--------------------------------------------------------------------------
	LOLIX_INTERFACE(max_size		, key_type		, (void)const);

	//--------------------------------------------------------------------------
	//	>> 2007-03-30 01:32:04 >>------------------->> 2007-03-30 01:32:04 >>
		//FUNC>>$	:	增量为多少
			//FUNC>>@1	:	[out]查询的返回结果
	//--------------------------------------------------------------------------
	LOLIX_INTERFACE(inc_size		, key_type		, (void)const);

	//--------------------------------------------------------------------------
	//	>> 2007-04-01 17:15:19 >>------------------->> 2007-04-01 17:15:22 >>
		//FUNC>>$	:	得到当前容器数据数量
			//FUNC>>@	:	[null]
				//FUNC>>#	:	返回当前容器数量
	//--------------------------------------------------------------------------
	LOLIX_INTERFACE(size			, key_type		, (void)const);

	//--------------------------------------------------------------------------
	//	>> 2007-04-01 17:16:03 >>------------------->> 2007-04-01 17:16:03 >>
		//FUNC>>$	:	得到当前容器能够不用新分配内存就可以容纳的数据总量
			//FUNC>>@	:	[null]
				//FUNC>>#	:	得到能够容纳的数量
	//--------------------------------------------------------------------------
	LOLIX_INTERFACE(capacity		, key_type		, (void)const);

	//--------------------------------------------------------------------------
	//	>> 2007-04-01 17:17:10 >>------------------->> 2007-04-01 17:17:10 >>
		//FUNC>>$	:	分配内存已达到能够不用重新分配就能够容纳的数据量
			//FUNC>>@	:	需要能够达到的数据
				//FUNC>>#	:	[null]
	//--------------------------------------------------------------------------
	LOLIX_INTERFACE(reserve			, void			, (key_type size));

	//--------------------------------------------------------------------------
	//	>> 2007-03-30 01:10:24 >>------------------->> 2007-03-30 01:10:24 >>
		//FUNC>>$	:	申请一个新的 ID, 并且将该 ID 绑定于当前数据的 开头
			//FUNC>>@0	:	[out]返回的ID
			//FUNC>>@1	:	[out]返回的数据地址
				//FUNC>>#	:	如果成功， 返回 LOLIX_RETNV_OK， 此时 key, 和 ptr 都是存在有效数据的
	//--------------------------------------------------------------------------
	LOLIX_INTERFACE(new_alloc_front	, LOLIX_RETNV	, (key_type* key, void**));

	//--------------------------------------------------------------------------
	//	>> 2007-03-30 01:10:24 >>------------------->> 2007-03-30 01:10:24 >>
		//FUNC>>$	:	申请一个新的 ID, 并且将该 ID 绑定于当前数据的 末尾
			//FUNC>>@0	:	[out]返回的ID
			//FUNC>>@1	:	[out]返回的数据地址
				//FUNC>>#	:	如果成功， 返回 LOLIX_RETNV_OK， 此时 key, 和 ptr 都是存在有效数据的
	//--------------------------------------------------------------------------
	LOLIX_INTERFACE(new_alloc_back	, LOLIX_RETNV	, (key_type* key, void**));
	//--------------------------------------------------------------------------
	//	>> 2007-03-30 01:32:04 >>------------------->> 2007-03-30 01:32:04 >>
		//FUNC>>$	:	通过关键字查找对象
			//FUNC>>@0	:	[in] 关键字
			//FUNC>>@1	:	[out]查询的返回结果（如果key 无效，则返回为空）
				//FUNC>>#	:	如果没有异常， 则返回  LOLIX_RETNV_OK
				//				key 无效， 则返回 LOLIX_ERROR_INVALID_PARAM
				//				此时 @1 返回为 0
	//--------------------------------------------------------------------------
	LOLIX_INTERFACE(get_value		, LOLIX_RETNV	, (key_type key, void** buf));

	//--------------------------------------------------------------------------
	//	>> 2007-03-30 01:34:54 >>------------------->> 2007-03-30 01:34:54 >>
		//FUNC>>$	:	得到当前关键字的下一个关键字
			//FUNC>>@0	:	[in] 当前关键字
			//FUNC>>@1	:	[out]下一个关键字
				//FUNC>>#	:	如果没有异常， 则返回 LOLIX_RETNV_OK
				//				如果 key 无效， 则返回 LOLIX_ERROR_INVALID_PARAM
				//				此时 next 为 未定义
	//--------------------------------------------------------------------------
	LOLIX_INTERFACE(get_next_key	, LOLIX_RETNV	, (key_type key, key_type* next));

	//--------------------------------------------------------------------------
	//	>> 2007-03-30 01:34:54 >>------------------->> 2007-03-30 01:34:54 >>
		//FUNC>>$	:	得到当前关键字的前一个关键字
			//FUNC>>@0	:	[in] 当前关键字
			//FUNC>>@1	:	[out]前一个关键字
				//FUNC>>#	:	如果没有异常， 则返回 LOLIX_RETNV_OK
				//				如果 key 无效， 则返回 LOLIX_ERROR_INVALID_PARAM
				//				此时 next 为 未定义
	//--------------------------------------------------------------------------
	LOLIX_INTERFACE(get_prev_key	, LOLIX_RETNV	, (key_type key, key_type* next));

	//--------------------------------------------------------------------------
	//	>> 2007-03-30 01:51:12 >>------------------->> 2007-03-30 01:51:12 >>
		//FUNC>>$	:	移除 关键字所对应的数据项
			//FUNC>>@0	:	[in]	关键字
			//FUNC>>@1	:	[out]	下一个关键字（返回值不等于 LOLIX_RETNV_OK 时未定义）
				//FUNC>>#	:	如果没有异常， 则返回 LOLIX_RETNV_OK
				//				如果 key 无效， 则返回 LOLIX_ERROR_INVALID_PARAM
	//--------------------------------------------------------------------------
	LOLIX_INTERFACE(erase			, LOLIX_RETNV	, (key_type key_begin, key_type key_end, size_type erased_size, key_type* next));

	//--------------------------------------------------------------------------
	//	>> 2007-03-30 02:12:43 >>------------------->> 2007-03-30 02:12:43 >>
		//FUNC>>$	:	得到组内最开始节点
			//FUNC>>@	:	[null]
				//FUNC>>#	:	返回头节点的 key, 如果为空, 则返回类同于 end() 操作
	//--------------------------------------------------------------------------
	LOLIX_INTERFACE(begin			, key_type		, (void)const);

	//--------------------------------------------------------------------------
	//	>> 2007-03-30 02:12:43 >>------------------->> 2007-03-30 02:12:43 >>
		//FUNC>>$	:	得到组内末尾的开区间
			//FUNC>>@	:	[null]
				//FUNC>>#	:	返回尾端 key
	//--------------------------------------------------------------------------
	LOLIX_INTERFACE(end				, key_type		, (void)const);
};



template<typename _Ty, typename _TKey = lolix::lx_u64>
class	lolix::toy::key_bind_dynamic
{
	typedef	key_bind_dynamic<_Ty, _TKey>	this_type;

	typedef key_bind_dynamic_handle::key_type	_T_ckey_type;
	typedef _TKey				_T_key_type;
	typedef	pair<const _T_key_type, _Ty&>	_T_value;
protected:
	key_bind_dynamic_handle*	_key_bind;
public:
	typedef	size_type			size_type;
	typedef	difference_type		difference_type;

	typedef	_T_value			value_type;
	typedef	_T_value const&		const_reference;
	typedef	_T_value const*		const_pointer;
	typedef	_T_value*			pointer;
	typedef	_T_value&			reference;
	typedef _T_key_type			key_type;
	typedef	_Ty					mapped_type;
	typedef	_Ty					referent_type;
	typedef	void				allocator_type;

	class	_T_citer;
	friend	_T_citer;
	class _T_citer
	{
	protected:
		typedef key_bind_dynamic<_Ty, _TKey>		contain_type;
		typedef	typename _TKey						key_type;
		friend contain_type;
		typedef _T_citer this_type;

		contain_type*	_id_bind;									//	容器
		_TKey			_key_value;
mutable	unsigned int	_init	: 1;								//	是否初始化过下面的内存区
		unsigned int	_rbegin	: 1;								//	是否是 rbegin : 逆相迭代器保留

		unsigned char _value_buf[sizeof contain_type::value_type];	//	value_type : 惰性计算的一个缓冲区

		_T_citer(contain_type* id_bind_ptr, key_type key)
			: _id_bind(id_bind_ptr), _key_value(key), _init(0){}

	public:
		typedef std::bidirectional_iterator_tag			iterator_category;
		typedef typename contain_type::const_pointer	const_pointer;
		typedef typename contain_type::const_pointer	pointer;
		typedef typename contain_type::const_reference	const_reference;
		typedef typename contain_type::const_reference	reference;
		typedef typename contain_type::value_type		value_type;

		_T_citer(void)
			: _id_bind(0)
			{
			}

		bool operator == (const this_type& o)const
			{
			return _key_value == o._key_value;
			}

		bool operator != (const this_type& o)const
			{
			return !((*this) == o);
			}

		this_type& operator ++(void)
			{
			_init = 0;
			LOLIX_ASSERT( *this != _id_bind->end(), L"在叠代器尾端继续++");
			_T_ckey_type key_val = _key_value;
			_id_bind->_key_bind->get_next_key(key_val, &key_val);
			_key_value = static_cast<key_type>(key_val & type::type_value<_TKey>::max_val);

			return *this;
			}
		const this_type operator ++ (int)
			{
			this_type tmp(*this);
			++(*this);
			return tmp;
			}

		this_type& operator --(void)
			{
			_init = 0;
			LOLIX_ASSERT(*this != _id_bind->begin(), L"在叠代器首端继续--");
			_T_ckey_type key_val = _key_value;
			_id_bind->_key_bind->get_prev_key(key_val, &key_val);
			_key_value = static_cast<key_type>(key_val & type::type_value<_TKey>::max_val);
			return *this;
			}
		const this_type operator -- (int)
			{
			this_type tmp(*this);
			--(*this);
			return tmp;
			}

		const_reference operator* (void)const
			{
			if ( !_init )
				{
				LOLIX_ASSERT( (*this) != _id_bind->end(), L"非法的反解除指针");
				void* ptr;
				_id_bind->_key_bind->get_value(_key_value, &ptr);
				new ((void*)_value_buf)	value_type(_key_value, *(typename contain_type::mapped_type*)ptr);
				_init = 1;
				}

			return *reinterpret_cast<const value_type*>(_value_buf);
			}

		const_pointer operator -> (void)const
			{
				return &(**this);
			}
	};

	class _T_iter : public key_bind_dynamic<_Ty, _TKey>::const_iterator
	{
		typedef _T_iter this_type;
		typedef typename key_bind_dynamic<_Ty, _TKey>::const_iterator base_type;
		typedef	typename base_type::contain_type	contain_type;
		friend contain_type;
		_T_iter(contain_type* id_bind_ptr, key_type key)
			: base_type(id_bind_ptr, key){}
	public:
		typedef typename contain_type::reference	reference;
		typedef typename contain_type::pointer	pointer;

		_T_iter(void){}
		reference operator* (void)const
			{
			return const_cast<reference>(base_type::operator *());
			}

		pointer operator -> (void)const
			{
			return &(**this);
			}
		this_type& operator++ (void)
			{
			base_type::operator++();
			return *this;
			}

		const this_type operator++(int)
			{
			this_type tmp(*this);
			++*this;
			return tmp;
			}

		this_type& operator-- (void)
			{
			base_type::operator--();
			return *this;
			}

		const this_type operator-- (int)
			{
			this_type tmp(*this);
			--*this;
			return tmp;
			}
	protected:
	};


	template<typename t_bidirectional_iterator>
	class _T_riter_t : protected t_bidirectional_iterator
	{
		typedef _T_riter_t<t_bidirectional_iterator>	this_type;
		typedef t_bidirectional_iterator				base_type;

		friend typename base_type::contain_type;
		_T_riter_t(base_type b_type, bool is_rend): base_type(b_type)
			{ if ( (_rbegin = is_rend) == false ) base_type::operator --(); }
	public:
		_T_riter_t(void){ }
		this_type& operator++ (void)
			{
			if ( base_type::operator == (_id_bind->begin()) )
				_rbegin = 1;
			else
				base_type::operator -- ();
			//this->__goto_prev__();
			return *this;
			}
		this_type& operator-- (void)
			{
			if ( _rbegin )
				_rbegin = 0;
			else
				base_type::operator ++ ();
			//this->__goto_next__();
			return *this;
			}
		const this_type operator++ (int)
			{
			this_type tmp(*this);
			++*this;
			return tmp;
			}
		const this_type operator-- (int)
			{
			this_type tmp(*this);
			--*this;
			return tmp;
			}

		using base_type::operator*;
		using base_type::operator->;
		bool operator==(this_type const& o)const{return _rbegin == o._rbegin && base_type::operator==(o);}
		bool operator!=(this_type const& o)const{return !(this_type::operator==(o));}
	};

	typedef	_T_citer					const_iterator;
	typedef	_T_iter						iterator;
	typedef	_T_riter_t<_T_citer>		_T_rciter;
	typedef	_T_rciter					const_reverse_iterator;
	typedef	_T_riter_t<_T_iter>			_T_riter;
	typedef	_T_riter					reverse_iterator;

	explicit key_bind_dynamic<_Ty, _TKey>( key_bind_dynamic_handle* handle )
		: _key_bind(handle)
	{
		_key_bind->inc_ref();
	}

	key_bind_dynamic<_Ty, _TKey>( key_bind_dynamic<_Ty, _TKey>& o )
		: _key_bind(o._key_bind)
	{
		_key_bind->inc_ref();
	}

	~key_bind_dynamic<_Ty, _TKey>(void)
	{
		_key_bind->release();
	}

	key_bind_dynamic<_Ty, _TKey>& operator = (key_bind_dynamic<_Ty, _TKey>& o)
	{
		this_type tmp(o);
		this->swap(tmp);
		return *this;
	}

	void swap(key_bind_dynamic<_Ty, _TKey>& o)
	{
		_key_bind* tmp = _key_bind;
		_key_bind = o._key_bind;
		o._key_bind = tmp;
	}

	inline	bool		empty(void)const			{ return !size(); }
	inline	_T_citer	begin(void)const			{ return const_cast<key_bind_dynamic<_Ty, _TKey>*>(this)->begin(); }
	inline	_T_citer	end(void)const				{ return const_cast<key_bind_dynamic<_Ty, _TKey>*>(this)->end(); }
	inline	_T_iter		begin(void)					{ return iterator(this, static_cast<key_type>(_key_bind->begin() & type::type_value<key_type>::max_val)); }
	inline	_T_iter		end(void)					{ return iterator(this, static_cast<key_type>(_key_bind->end() & type::type_value<key_type>::max_val)); }
	inline	_T_riter	rbegin(void)				{ return _T_riter(this->end(), 0); }
	inline	_T_rciter	rbegin(void)const			{ return _T_riter(this->end(), 0); }
	inline	_T_riter	rend(void)					{ return _T_riter(this->begin(), 1); }
	inline	_T_rciter	rend(void)const				{ return _T_riter(this->begin(), 1); }
	inline	size_type	size(void)const				{ return static_cast<size_type>(_key_bind->size()); }
	inline	void		clear(void)					{ erase(begin(), end()); }
	inline	_T_iter		erase(_T_iter b, _T_iter e)
	{
		size_type ecnt = 0;
		for ( _T_iter be(b),ed(e) ; be != ed; ++be, ++ecnt )
			__Destory(be->first);

		key_bind_dynamic_handle::key_type kval;
		LOLIX_CHECK_ERROR(_key_bind->erase(b._key_value, e._key_value, ecnt, &kval));
		return _T_iter(this, static_cast<key_type>(kval & type::type_value<key_type>::max_val));
	}


	inline	_T_iter		erase(_T_iter iter)
	{
		_T_iter next = iter;
		if ( iter == this->end() )
			return iter;

		++next;
		return erase(iter, next);
	}

	inline	size_type	erase(key_type key)
	{
		erase(_T_iter(this, key)); return 1;
	}


	inline	_T_iter		find(key_type key)			{ void* tmp; return LOLIX_IS_ERROR(_key_bind->get_value(key, &tmp))?end():iterator(this, key); }
	inline	_T_citer	find(key_type key)const		{ return const_cast<key_bind_dynamic*>(this)->find(key); }

	inline	iterator push_back(const mapped_type& v_type)
	{
		_T_ckey_type k;
		void* ptr;
		if ( !LOLIX_IS_ERROR(_key_bind->new_alloc_back(&k, &ptr)) )
			new (ptr) _Ty(v_type);
		return iterator(this, static_cast<key_type>(k & type::type_value<key_type>::max_val));
	}

	inline	iterator push_front(const mapped_type& v_type)
	{
		_T_ckey_type k;
		void* ptr;
		if ( !LOLIX_IS_ERROR(_key_bind->new_alloc_front(&k, &ptr)) )
			new (ptr) _Ty(v_type);

		return iterator(this, static_cast<key_type>(k & type::type_value<key_type>::max_val));
	}


	LOLIX_INLINE_CALL	size_type	capacity(void)const
	{
		return (size_type)_key_bind->capacity();
	}

	LOLIX_INLINE_CALL	mapped_type const&	operator[](key_type k)const
	{
		return static_cast<this_type*>(this)->operator[](k);
	}

	LOLIX_INLINE_CALL	mapped_type&	operator[](key_type k)
	{
		void* ptr;
		LOLIX_CHECK_ERROR(_key_bind->get_value(k, &ptr));
		return *(mapped_type*)ptr;
	}

	LOLIX_INLINE_CALL	mapped_type const&	operator[](iterator i)const
	{
		return static_cast<this_type*>(this)->operator[](i->first);
	}

	LOLIX_INLINE_CALL	mapped_type&	operator[](iterator k)
	{
		return operator[](k->first);
	}
private:
	inline	void __Destory(key_type key)
	{
		void* ptr;
#if			LOLIX_CFG__IS_DEBUG
		LOLIX_CHECK_ERROR(_key_bind->get_value(key, &ptr));
#else	//	LOLIX_CFG__IS_DEBUG
		_key_bind->get_value(key, &ptr);
#endif	//	LOLIX_CFG__IS_DEBUG
		static_cast<_Ty*>(ptr)->~_Ty();
	}
};


#undef	__LOLIX_CHECK_ID_BIND_VALID__

SYS_REGIST_TYPE(lolix::toy::key_bind_dynamic_handle);
SYS_REGIST_TYPE(lolix::toy::key_bind_dynamic_handle::factory_type);

namespace lolix{
	template<typename bind_type, size_type max_element_size, size_type row_size_input, typename _T_ALLOC, typename need_adjust_free_prev_pointer>
	LOLIX_INLINE_CALL void swap(toy::key_bind<bind_type, max_element_size, row_size_input, _T_ALLOC, need_adjust_free_prev_pointer>& tl, toy::key_bind<bind_type, max_element_size, row_size_input, _T_ALLOC, need_adjust_free_prev_pointer>& tr)
		{ tl.swap(tr); }

	template <typename bind_type, lolix::size_type max_element_size, lolix::size_type row_size_input, typename _T_ALLOC>
	LOLIX_INLINE_CALL void swap(toy::key_bind_random<bind_type, max_element_size, row_size_input, _T_ALLOC>& tl, toy::key_bind_random<bind_type, max_element_size, row_size_input, _T_ALLOC>& tr)
		{ tl.swap(tr); }
}
#endif	//	!defined(__LOLIX__TOY__KEY_BIND__K_IN_G__)
